#include "stdafx.h"
#include "Cell.h"
#include "GameBoard.h"
#include <iostream>
#include <cstdlib>	//For the rand and srand functions, and for the system("cls") function
#include <ctime>	//For the time function
using namespace std;


/************************************************************************************************
 *	The constructor for the gameboard takes a user input from main for the size of the board.	*
 *	It then generates the board and assigns random numbers to each cell in the board by			*
 *		using the rand and srand and time functions.											*
 ************************************************************************************************/
GameBoard::GameBoard(int size)
{
	int count1 = 0, count2 = 0;	//The counters for generating the random numbers loop

	//Get the system time
	int seed = time(0);
	//Seed the random number generator
	srand(seed);
	
	_masterptr = new Cell*[size];	 //Creates a new array of pointers to Cell objects
	for(int i=0; i<size; ++i)		//We have to create an array of pointers to an array of Cells to get this to work
	_masterptr[i]=new Cell[size];	//This syntax is VERY confusing, but it allows us to acces the array now using _masterptr[][] notation.
									//I had to do this because you cannot create a new 2 dimensional array, only 1 dimensional.
	
	//*******************************************************
	//Then we will populate the board with random numbers	*
	//*******************************************************
	for(count1 = 0; count1 < size; count1++)
	{
		for(count2 = 0; count2 < size; count2++)
		{
			_masterptr[count1][count2].SetColor((rand() % 6));	//This is generating a random number between 0 and 5
		}
	}

	//*******************************************************
	//We will set pointers in the first row
	//*******************************************************
	count1 = 0;
	
		for(count2 = 0; count2 < size-1; count2++)
		{
			_masterptr[count1][count2].SetpRight(&_masterptr[count1][count2+1]);	 
																			
		}
		for(count2 = 0; count2 < size; count2++)
		{
			_masterptr[count1][count2].SetpBelow(&_masterptr[count1+1][count2]);	
																			
		}
		for(count2 = 1; count2 < size; count2++)
		{
			_masterptr[count1][count2].SetpLeft(&_masterptr[count1][count2-1]);
		}
	
	//*******************************************************
	//We will set pointers in the interior rows
	//*******************************************************
	
	for(count1 = 1; count1 < size-1; count1++)
	{
			for(count2 = 1; count2 < size-1; count2++)
			{
			_masterptr[count1][count2].SetpRight(&_masterptr[count1][count2+1]);	
			_masterptr[count1][count2].SetpBelow(&_masterptr[count1+1][count2]);	
			_masterptr[count1][count2].SetpLeft(&_masterptr[count1][count2-1]);
			_masterptr[count1][count2].SetpAbove(&_masterptr[count1-1][count2]);
			}
			count2 =0;//leftmost edge
			_masterptr[count1][count2].SetpRight(&_masterptr[count1][count2+1]);	
			_masterptr[count1][count2].SetpBelow(&_masterptr[count1+1][count2]);	
			_masterptr[count1][count2].SetpAbove(&_masterptr[count1-1][count2-1]);

			count2 =size-1;//rightmost edge
			_masterptr[count1][count2].SetpBelow(&_masterptr[count1+1][count2]);	
			_masterptr[count1][count2].SetpLeft(&_masterptr[count1][count2-1]);
			_masterptr[count1][count2].SetpAbove(&_masterptr[count1-1][count2]);
	
	}
	//*******************************************************
	//We will set pointers in the last row
	//*******************************************************
	count1 = size -1;
		for(count2 = 0; count2 < size-1; count2++)
		{
			_masterptr[count1][count2].SetpRight(&_masterptr[count1][count2+1]);	 
																			
		}
		for(count2 = 0; count2 < size; count2++)
		{
			_masterptr[count1][count2].SetpAbove(&_masterptr[count1-1][count2]);	
																			
		}
		for(count2 = 1; count2 < size; count2++)
		{
			_masterptr[count1][count2].SetpLeft(&_masterptr[count1][count2-1]);
		}
		_masterptr[0][0].SetFlooded(true);
		_masterptr[0][1].SetNextTo(true);
		_masterptr[1][0].SetNextTo(true);

}


/********************************************************************************************************
 *	This function will display the board in full. It takes in the size of the board as an argument		*
 ********************************************************************************************************/
void GameBoard::DisplayBoard(int size)
{
	system("cls");		//Clears the screen
	int count1 = 0, count2 = 0;	//The counters for displaying the board
	cout << "\n\n\n";	//Format it into about the middle of the screen

		for(count1 = 0; count1 < size; count1++)	//Display the board one element at a time
		{
			cout << "\t\t";	//Format it into about the middle of the screen
			for(count2 = 0; count2 < size; count2++)
			{
				cout << _masterptr[count1][count2].GetColor() << " ";
			}
			cout << endl;
		}
}
void GameBoard::FloodGrid(int newcolor,int size)
{
	int count1 = 0, count2 = 0;
	if(_masterptr[0][0].GetColor()!=newcolor)//check to make sure newcolor is truly a new color
		for(count1 = 0; count1 < size; count1++)	//Display the board one element at a time
		{
			for(count2 = 0; count2 < size; count2++)
			{
				if(_masterptr[count1][count2].GetFlooded()||_masterptr[count1][count2].GetNextTo())
					_masterptr[count1][count2].SetColor(newcolor);

			}
		
		}
}